//
//  XMEvaluateView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/6/11.
//

#import "XMEvaluateView.h"

@interface XMEvaluateView ()

@property (nonatomic, strong) UIImageView *avatarImgView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *imgBgView;
@property (nonatomic, strong) UILabel *dateLabel;

@property (nonatomic, strong) NSMutableArray *likeBtnArr;
@property (nonatomic, strong) NSMutableArray *imgArr;

@end

@implementation XMEvaluateView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.avatarImgView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.dateLabel];
    [self addSubview:self.titleLabel];
    [self addSubview:self.imgBgView];
    
    if (self.likeBtnArr.count == 0) {
        for (int i = 0; i < 5; i ++) {
            UIButton *likeBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(_nameLabel.x + (KScaleWidth(13.0) + KScaleWidth(5.0))*i, _nameLabel.maxY + KScaleWidth(8.0), KScaleWidth(13.0), KScaleWidth(12.0)) image:@"evaluate_like_icon" selectImg:@"evaluate_like_icon_select"];
            [self addSubview:likeBtn];
            [self.likeBtnArr addObject:likeBtn];
        }
    }
    CGFloat space = (_imgBgView.width - KScaleWidth(100.0)*3)/2;
    if (self.imgArr.count == 0) {
        for (int i = 0; i < 9; i ++) {
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake((KScaleWidth(100.0) + space)*(i%3), (KScaleWidth(104) + KScaleWidth(8.0))*(i/3), KScaleWidth(100.0), KScaleWidth(104.0))];
            [_imgBgView addSubview:imgView];
            [self.imgArr addObject:imgView];
        }
    }
}

- (void)setContentWithEvaluateModel:(XMCommentModel *)evaluateModel {
    [_avatarImgView sd_setImageWithURL:[NSURL URLWithString:evaluateModel.avatar] placeholderImage:ImageNamed(@"")];
    _avatarImgView.image = ImageNamed(@"mine_avatar_icon");
    _nameLabel.text = evaluateModel.nickname;//@"唐小图";
    for (int i = 0; i < 5; i ++) {
        UIButton *likeBtn = self.likeBtnArr[i];
        if (i < 3) {
            likeBtn.selected = YES;
        }
        else {
            likeBtn.selected = NO;
        }
    }
    _titleLabel.text = evaluateModel.content;//@"专业个人化妆师，我，以对时尚的敏锐洞察，";
    CGFloat titleHeight = [_titleLabel.text getHeightWithFont:KScaleWidth(14.0) withWidth:_titleLabel.width] + KScaleWidth(10.0);
    _titleLabel.height = MAX(KScaleWidth(28.0), titleHeight);
    _imgBgView.y = _titleLabel.maxY;
    for (int i = 0; i < 9; i ++) {
        UIImageView *imgView = self.imgArr[i];
        imgView.image = nil;
    }
    self.height = _titleLabel.maxY;
}

#pragma mark - Init
- (UIImageView *)avatarImgView {
    if (!_avatarImgView) {
        _avatarImgView = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(8.0), KScaleWidth(KScaleWidth(8.0)), KScaleWidth(36.0), KScaleWidth(36.0))];
        [_avatarImgView setCornerRadius:_avatarImgView.height/2];
    }
    return _avatarImgView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.avatarImgView.maxX + KScaleWidth(6.0), _avatarImgView.y, kScreenWidth/2 - KScaleWidth(6.0) - _avatarImgView.maxX, KScaleWidth(30.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
    }
    return _nameLabel;
}

- (UILabel *)dateLabel {
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth/2, self.nameLabel.y, kScreenWidth/2 - KScaleWidth(13.0), _nameLabel.height) font:KFontSize(14.0) textColor:kColorHEX(0x929496)];
    }
    return _dateLabel;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLabel.x, _nameLabel.maxY + KScaleWidth(20.0), kScreenWidth - _nameLabel.x - KScaleWidth(13.0), KScaleWidth(28.0)) font:KFontSize(14.0) textColor:kColorHEX(0x666666)];
    }
    return _titleLabel;
}

- (UIView *)imgBgView {
    if (!_imgBgView) {
        _imgBgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.titleLabel.x, _titleLabel.maxY, _titleLabel.width, KScaleWidth(104.0))];
    }
    return _imgBgView;
}

- (NSMutableArray *)likeBtnArr {
    if (!_likeBtnArr) {
        _likeBtnArr = [NSMutableArray array];
    }
    return _likeBtnArr;
}

- (NSMutableArray *)imgArr {
    if (!_imgArr) {
        _imgArr = [NSMutableArray array];
    }
    return _imgArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
